<?php
declare(strict_types=1);

namespace app\admin\business;

use app\common\model\FeedbackForm as FeedbackFormModel;

class FeedbackForm extends AdminBase
{
    private $_feedbackFormObj = null;

    public function __construct(string $websiteCode)
    {
        $this->_websiteCode     = $websiteCode;
        $this->_feedbackFormObj = new FeedbackFormModel([], $websiteCode);
    }

    public function getAllFeedbackForm(array $where = []): array
    {
        $rows = [];
        try {
            $rows = $this->_feedbackFormObj->getAllFeedbackForm($where);
        } catch (\Exception $e) {
            logErr('读取所有自定义表单失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return $rows ? $rows->toArray() : [];
    }

    public function getFeedbackFormList(int $page = 1, int $limit = 10, array $where = []): array
    {
        $rows  = null;
        $count = 0;
        try {
            $rows  = $this->_feedbackFormObj->getFeedbackFormList($page, $limit, $where);
            $count = $this->_feedbackFormObj->getFeedbackFormCount($where);
        } catch (\Exception $e) {
            logErr('读取自定义表单列表失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return ['rows' => $rows ? $rows->toArray() : [], 'count' => $count];
    }

    public function addFeedbackForm(array $data): array
    {
        try {
            return [self::_RESULT => $this->_feedbackFormObj->addFeedbackForm($data) && $this->_feedbackFormObj->createTable($data), self::_MESSAGE => self::_ADD_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('添加自定义表单失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }

    public function findFeedbackFormById(int $id): array
    {
        $feedbackForm = null;
        try {
            $feedbackForm = $this->_feedbackFormObj->findFeedbackFormById($id);
        } catch (\Exception $e) {
            logErr('通过主键读取自定义表单详细信息失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        if (empty($feedbackForm)) {
            return [];
        }

        return $feedbackForm->toArray();
    }

    public function updateFeedbackForm(int $id, array $data): array
    {
        try {
            return [self::_RESULT => $this->_feedbackFormObj->updateFeedbackForm($id, $data), self::_MESSAGE => self::_UPDATE_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('通过主键更新自定义表单信息失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }

    public function deleteFeedbackForm(int $id): array
    {
        try {
            $row = $this->findFeedbackFormById($id);
            if (empty($row)) {
                return [self::_RESULT => true, self::_MESSAGE => self::_DELETE_SUCCESS_MESSAGE];
            }

            return [self::_RESULT => $this->_feedbackFormObj->deleteFeedbackForm($id) && $this->_feedbackFormObj->dropTable($row), self::_MESSAGE => self::_DELETE_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('通过主键删除自定义表单失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }
}
